package com.siriuscloud.holidayreservation.web;

import com.siriuscloud.holidayreservation.domain.AppointNumber;
import com.siriuscloud.holidayreservation.domain.Holiday;
import com.siriuscloud.holidayreservation.domain.vo.HolidayVo;
import com.siriuscloud.holidayreservation.service.AppointNumberService;
import com.siriuscloud.holidayreservation.service.HolidayService;
import com.siriuscloud.holidayreservation.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class HolidayController {
    @Resource
    private HolidayService holidayService;

    @Resource
    private AppointNumberService appointNumberService;

    @GetMapping("/holiday")
    public R list(
            @RequestParam String startTime,
            @RequestParam String endTime,
            @RequestParam(required = false) Integer type) throws Exception {
        try {
            Date start = null;
            if (!StringUtils.isEmpty(startTime)) {
                start = new SimpleDateFormat("yyyy-MM-dd").parse(startTime);
            }
            Date end = null;
            if (!StringUtils.isEmpty(endTime)) {
                end = new SimpleDateFormat("yyyy-MM-dd").parse(endTime);
            }
            List<Holiday> holidayList = holidayService.getAllHoliday(start, end, type);
            return R.ok().data(holidayList);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @GetMapping("/appoint-number")
    public R list(
            @RequestParam String startTime,
            @RequestParam String endTime,
            @RequestParam(required = false) String departmentId) throws Exception {
        Date start = null;
        if (!StringUtils.isEmpty(startTime)) {
            start = new SimpleDateFormat("yyyy-MM-dd").parse(startTime);
        }
        Date end = null;
        if (!StringUtils.isEmpty(endTime)) {
            end = new SimpleDateFormat("yyyy-MM-dd").parse(endTime);
        }
        List<AppointNumber> appointNumbers = appointNumberService.getAllHoliday(start, end, departmentId);
        return R.ok().data(appointNumbers);
    }

    @PostMapping("/holiday")
    public R add(@RequestBody HolidayVo holiday) {
        Boolean flag = holidayService.create(holiday);
        if (flag) {
            return R.ok().msg("操作成功");
        }
        return R.error().msg("操作失败");
    }

    @PutMapping("/holiday")
    public R update(@RequestBody Holiday holiday) {
        Boolean flag = holidayService.update(holiday);
        if (flag) {
            return R.ok().msg("修改成功");
        }
        return R.error().msg("修改失败");
    }

}
